﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SMSCollaboration
{
    public partial class Form1 : Form
    {
        public string com;
        public string myflowcontrol;
        public string[] strarray = new string[10];
        public int mybaudrate;
        public int mydatabit;
        public int mystopbit;
        public int cb1;
        public string fm;
        public int number2 = 160;
        public string servicecenter;
        public string myshow;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //name of all ports are copied
            strarray = System.IO.Ports.SerialPort.GetPortNames();
            // items of combobox1 are initialized
            this.comboBox1.Items.AddRange(strarray);

        }

        private void Label27_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //sms sending number

        }

        public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            com = comboBox1.Text;
            //MessageBox.Show(com); showing the port selected by user at run time.

        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {

                if (!this.serialPort1.IsOpen)
                {

                    this.serialPort1.PortName = com;

                    this.serialPort1.Open();
                    this.serialPort1.BaudRate = mybaudrate;
                    this.serialPort1.DataBits = 8;
                    this.serialPort1.StopBits = System.IO.Ports.StopBits.One;
                    this.serialPort1.Parity = System.IO.Ports.Parity.None;
                    this.serialPort1.Handshake = System.IO.Ports.Handshake.None;

                    MessageBox.Show("\nCONNECTED SUCESSFULLY\n", "Information");


                }

                else
                {
                    MessageBox.Show("\nALLREADY CONNECTED\n", "Information");
                }
            }
            catch
            {
                MessageBox.Show("\nCOMMPORT IS NOT CONNECTED", "Information");

            }



        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            mybaudrate = Convert.ToInt32(comboBox2.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                this.serialPort1.Close();
                MessageBox.Show("\nDisconnected Successfully\n", "Information");
            }

            else
            {
                MessageBox.Show("\nALLREADY DISCONNECTED\n", "Information");
            }

        }

        public void button3_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {

                serialPort1.BaseStream.Flush();

                string cb = char.ConvertFromUtf32(26);

                System.Threading.Thread.Sleep(2000);

                this.serialPort1.Write("AT+CMGF=1\r");
                this.serialPort1.Write("AT+CSCA=servicecenter\r\n");//Ufone Service Center

                this.serialPort1.Write("AT+CMGS=\"" + textBox1.Text + "\"\r\n");// message sending
                this.serialPort1.Write(textBox2.Text + cb);//message text

                MessageBox.Show("\nMessage Sent\n", "Information");
            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            // text box, inwhich you write the SMS
            this.textBox2.MaxLength = 160;
            number2--;
            this.textBox4.Text = number2.ToString();  // number of characters remaining in a sms
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {

                // to send bulk sms


                serialPort1.BaseStream.Flush();

                int loop = 0;
                int howmany = 0;

                howmany = Convert.ToInt32(textBox5.Text);



                while (loop < howmany)
                {

                    System.Threading.Thread.Sleep(3500);

                    string cb = char.ConvertFromUtf32(26);
                    this.serialPort1.Write("AT+CMGF=1\r");
                    this.serialPort1.Write("AT+CSCA=servicecenter\r\n");//Ufone Service Center
                    this.serialPort1.Write("AT+CMGS=\"" + textBox1.Text + "\"\r\n");// message sending
                    this.serialPort1.Write(textBox2.Text + cb);//message text

                    System.Threading.Thread.Sleep(3500);

                    loop++;
                }

                MessageBox.Show("\nBulk Messages Successfully Sent\n", "Information");
            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //to make a call
            if (this.serialPort1.IsOpen)
            {

                serialPort1.BaseStream.Flush();
                serialPort1.Write("ATD=" + textBox3.Text + ";\r\n");
            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }
        }


        private void button8_Click(object sender, EventArgs e)
        {
            // for a misscall
            if (this.serialPort1.IsOpen)
            {
                serialPort1.Write("ATD=" + textBox3.Text + ";\r\n");
                System.Threading.Thread.Sleep(7300);
                this.serialPort1.Write("ATH");
            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            //to disconnect from a call
            if (this.serialPort1.IsOpen)
            {
                serialPort1.BaseStream.Flush();
                this.serialPort1.Write("ATH");
            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {


        }

        private void button10_Click(object sender, EventArgs e)
        {
            //SENDING MESSAGE TO MULTIPLE RECEPIENT
            if (this.serialPort1.IsOpen)
            {
                string[] readlines2 = textBox6.Text.Split(';');

                for (int i = 0; i < readlines2.GetUpperBound(0); i++)
                {
                    this.serialPort1.DiscardInBuffer();

                    this.serialPort1.Write("AT+CMGF=1\r");
                    this.serialPort1.Write("AT+CSCA=servicecenter\r\n");//Ufone Service Center
                    string cb = char.ConvertFromUtf32(26);

                    System.Threading.Thread.Sleep(3500);

                    this.serialPort1.Write("AT+CMGS=\"" + readlines2[i] + "\"\r\n");// message sending
                    this.serialPort1.Write(textBox2.Text + cb);//message text

                    System.Threading.Thread.Sleep(2500);

                }

                MessageBox.Show("\nBulk Messages Sucessfully Sent\n", "Information");
            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {

            //IMEI Number
            if (this.serialPort1.IsOpen)
            {
                this.serialPort1.DiscardInBuffer();

                this.serialPort1.Write("AT+CGSN\r\n");

            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            //battery status


            if (this.serialPort1.IsOpen)
            {
                this.serialPort1.DiscardInBuffer();

                this.serialPort1.Write("AT+CBC\r\n");
            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            //delayed sms DATE AND TIME PICKER

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void button13_Click(object sender, EventArgs e)
        {
            //model and manufacturing

            if (this.serialPort1.IsOpen)
            {
                this.serialPort1.Write("at+cgmi\r\n");

            }


            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }


        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            fm = this.serialPort1.ReadExisting();
            MessageBox.Show(fm, "Information");
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            //service centre number
            string what;
            what = this.comboBox3.Text;

            if (what.CompareTo("Ufone") == 0)
            {
                //MessageBox.Show("Ufone");
                servicecenter = "+923330005150";
            }

            else if (what.CompareTo("Zong") == 0)
            {
                //MessageBox.Show("Zong");
                servicecenter = "+923040000011";
            }

            else if (what.CompareTo("Mobilink") == 0)
            {
                //MessageBox.Show("Mobilink");
                servicecenter = "+92300000042";

            }

            else if (what.CompareTo("Telenor") == 0)
            {
                //MessageBox.Show("Telenor");
                servicecenter = "+92345000010";
            }

            else if (what.CompareTo("Warid") == 0)
            {
                //MessageBox.Show("Warid");
                servicecenter = "+923210006001";
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            //help

            MessageBox.Show("1. COM port is the port on which your Cell Phone is connected." + Environment.NewLine + Environment.NewLine + "2. Baudrate varies from phone to phone, you can check on internet for your cell phone Baudrate." + Environment.NewLine + Environment.NewLine + "3. First choose your COM port, then your Baud Rate, and your Service center, after doing this Press Connect." + Environment.NewLine + Environment.NewLine + "4. Bulk Sms will send the multiple number of SMS to a single person, you have to enter the number in [NO. Of Times box], E.g If you want to send sms 10 times to 03451234567 enter 10 in No of times box and press Send Bulk Sms." + Environment.NewLine + Environment.NewLine + "5. Multilple Recipent will send your text sms to multiple recipents, you have to use semicollon (;) to separate number's. E.g In multiple recipent text box right (03451234567;03001234567;03211234567;) to send sms to these 3 numbers." + Environment.NewLine + Environment.NewLine + "Last but not least, this program is not 100% error free, I have only tested this program on Motorolla L6 and it will not work on every mobile, E.g This program will not run on NOKIA 1112 etc...." + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Maniac By Fowz", "HELP");
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {
            this.textBox2.MaxLength = 160;//maximum numbers of characters
            number2--;
            this.textBox4.Text = number2.ToString();//remaining numbers of characters 
        }

        private void button7_Click(object sender, EventArgs e)
        {


            if (this.serialPort1.IsOpen)
            {
                this.serialPort1.DiscardInBuffer();

                this.serialPort1.Write("AT+CMGL=\"REC UNREAD\"\r\n");
            }

            else
            {
                MessageBox.Show("\nSerial Port Is Not Open\n", " ERROR ");
            }
        }

    }
}
